package com.og.managedbean;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;

import com.og.backingbean.TimeSheet;
import com.og.backingbean.TimeSheetDate;
import com.og.dao.CRUDEntityDAO;

/**
 * @author Renaud DIANA
 *
 */
@Named
@SessionScoped
public class TimeSheetBean implements Serializable {

	private static final long serialVersionUID = 1L;

	// Log4j	 
	private static final Logger	log	= Logger.getLogger(TimeSheetBean.class);

	/* Dependency injection */
	// Model
    @Inject
    private CRUDEntityDAO<TimeSheet> timeSheetDAO;
    @Inject
    private CRUDEntityDAO<TimeSheetDate> timeSheetDateDAO;
    // BackingBean        
    @Inject
    private TimeSheet timeSheetNew;
    @Inject
    private TimeSheet timeSheetUpdate;
    @Inject
    private TimeSheet timeSheetView;
    @Inject
    private TimeSheetDate timeSheetDateNew;
    @Inject
    private TimeSheetDate timeSheetDateUpdate;
    @Inject
    private TimeSheetDate timeSheetDateView;
    @Inject
    private ListBean list;
        
    /* -------------------- */
    
	/**
	 * Default constructor
	 */
	public TimeSheetBean() {
		// TODO Auto-generated constructor stub
	}
	
	//**************************************** TimeSheet ****************************************

	public String create() {
		
		// Call model		
		timeSheetDAO.createEntity(timeSheetNew);
		
		// Generate new list
		list.findAllTimeSheet();
		
		// Clear form
		timeSheetNew = new TimeSheet();
						
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create timeSheet Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}	
		
	public String update() {
		
		// Call model				
		timeSheetDAO.updateEntity(timeSheetUpdate);
		
		// Toggle view
		timeSheetView = timeSheetUpdate;
				
		// Generate new list
		list.findAllTimeSheet();
			
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Update timeSheet Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String delete() {
				
		// Call model				
		timeSheetDAO.deleteEntity(timeSheetView);
		
		// Generate new list
		list.findAllTimeSheet();
		
		// Clear form
		timeSheetView = new TimeSheet();
				
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete timeSheet Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String findOne() {
		
		// Call model				
		timeSheetView = timeSheetDAO.findOneEntity(timeSheetView, timeSheetView.getIdTimeSheet());
					
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Find timeSheet Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}

	public String toggleUpdate() {
		
		timeSheetUpdate = timeSheetView;
		
		return "";
	}

	//**************************************** TimeSheetDate ****************************************
	
	public String createDate() {
		
		// Call model		
		timeSheetDateDAO.createEntity(timeSheetDateNew);
		
		// Generate new list
		list.findAllTimeSheetDate();
		
		// Update current list for page refresh
		List<TimeSheetDate> tsd = timeSheetView.getListTimeSheetDate();
		tsd.add(timeSheetDateNew);
		timeSheetView.setListTimeSheetDate(tsd);
		
		// Clear form
		timeSheetDateNew = new TimeSheetDate();
						
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create timeSheetDate Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}	
	
	public String updateDate() {
		
		// Call model				
		timeSheetDateDAO.updateEntity(timeSheetDateUpdate);
		
		// Toggle view
		timeSheetDateView = timeSheetDateUpdate;
				
		// Generate new list
		list.findAllTimeSheetDate();
			
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Update timeSheetDate Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String deleteDate() {
				
		// Call model				
		timeSheetDateDAO.deleteEntity(timeSheetDateView);
		
		// Generate new list
		list.findAllTimeSheetDate();
		
		// Clear form
		timeSheetDateView = new TimeSheetDate();
				
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete timeSheetDate Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String findOneDate() {
		
		// Call model				
		timeSheetDateView = timeSheetDateDAO.findOneEntity(timeSheetDateView, timeSheetDateView.getIdTimeSheetDate());
					
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Find timeSheetDate Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}

	public String toggleUpdateDate() {
		
		timeSheetDateUpdate = timeSheetDateView;
		
		return "";
	}


	// Getters and Setters
	
	public TimeSheet getTimeSheetNew() {
		return timeSheetNew;
	}

	public void setTimeSheetNew(TimeSheet timeSheetNew) {
		this.timeSheetNew = timeSheetNew;
	}

	public TimeSheet getTimeSheetUpdate() {
		return timeSheetUpdate;
	}

	public void setTimeSheetUpdate(TimeSheet timeSheetUpdate) {
		this.timeSheetUpdate = timeSheetUpdate;
	}

	public TimeSheet getTimeSheetView() {
		return timeSheetView;
	}

	public void setTimeSheetView(TimeSheet timeSheetView) {
		this.timeSheetView = timeSheetView;
	}

	public TimeSheetDate getTimeSheetDateNew() {
		return timeSheetDateNew;
	}

	public void setTimeSheetDateNew(TimeSheetDate timeSheetDateNew) {
		this.timeSheetDateNew = timeSheetDateNew;
	}

	public TimeSheetDate getTimeSheetDateUpdate() {
		return timeSheetDateUpdate;
	}

	public void setTimeSheetDateUpdate(TimeSheetDate timeSheetDateUpdate) {
		this.timeSheetDateUpdate = timeSheetDateUpdate;
	}

	public TimeSheetDate getTimeSheetDateView() {
		return timeSheetDateView;
	}

	public void setTimeSheetDateView(TimeSheetDate timeSheetDateView) {
		this.timeSheetDateView = timeSheetDateView;
	}
	
}
